//
//  BBTestAppView.m
//  BBTouch
//
//  Created by ben smith on 8/19/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBTestAppView.h"
#import "BBMeshController.h"
#import "BBConfigurationController.h"
#import "BBSimpleBlobTracker.h"
#import "BBBlobEvent.h"
#import "NSView+BBViewAdditions.h"

@implementation BBTestAppView

@synthesize backgroundGradient;

- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		tracker = [[BBSimpleBlobTracker alloc] init];
		[tracker setDelegate:self];
		[self setupBackgroundGradient];
	}
	return self;
}


-(void)blobCycleDone
{
	// do the thing
	[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect 
{
	[self.backgroundGradient drawInRect:self.bounds angle:90.0];
		
	[self drawBlobs];
	[self drawText:@"Test View" centeredAtPoint:NSMakePoint(NSMidX([self bounds]), 10)];
}

-(void)drawBlobs
{
	NSArray * blobs = [tracker blobPoints];
	for (BBBlobEvent* event in blobs) {
		[self drawBlobEvent:event];
	}
}

-(void)drawBlobEvent:(BBBlobEvent*)theBlobEvent
{
	float radius = [theBlobEvent radius] * 2.0;

	float scaleX = NSWidth([self bounds]);
	float scaleY = NSHeight([self bounds]);
	
	NSPoint bLoc = [theBlobEvent screenLocation];
	
	bLoc.x *= scaleX;
	bLoc.y *= scaleY;
	//radius *= scaleX;
	// blob location is in a normalizes coordinate, so from 0.0 - 1.0
	NSRect blobRect = NSMakeRect(bLoc.x - radius, bLoc.y - radius, radius * 2.0, radius * 2.0);
	// make a pretty oval in a rectangle. oooh!
	[[NSColor greenColor] set];
	[NSBezierPath strokeRect:blobRect];
	[[NSColor redColor] set];
	[[NSBezierPath bezierPathWithOvalInRect:blobRect] stroke];
	// now draw a cross hair, because, why not?
	[[NSColor whiteColor] set];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(bLoc.x, bLoc.y - radius * 2.5) toPoint:NSMakePoint(bLoc.x, bLoc.y + radius * 2.5)];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(bLoc.x - radius * 2.5,bLoc.y) toPoint:NSMakePoint(bLoc.x + radius * 2.5,bLoc.y)];
	
	[self drawText:[NSString stringWithFormat:@"#%d r:%3.1f",[theBlobEvent blobID],[theBlobEvent radius]] centeredAtPoint:NSMakePoint(bLoc.x, bLoc.y + radius + 12)];
}

- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	
	CGFloat red1   =    0.0 / 255.0;
	CGFloat green1 =   0.0 / 255.0;
	CGFloat blue1  =  0.0 / 255.0;
	
	CGFloat red2    =  30.0 / 255.0;
	CGFloat green2  =  50.0 / 255.0;
	CGFloat blue2   =  50.0 / 255.0;
	
	NSColor* gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	NSColor* gradientBottom = [NSColor colorWithCalibratedRed:red2 green:green2 blue:blue2 alpha:1.0];
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}


@end
